package com.copa.gamestore.module.user.controller;

import com.copa.gamestore.common.response.ResponseVo;
import com.copa.gamestore.module.user.domain.UserAccount;
import com.copa.gamestore.module.user.domain.dto.UserDto;
import com.copa.gamestore.module.user.application.UserApplicationService;
import com.copa.gamestore.module.user.domain.User;
import com.copa.gamestore.security.annotation.Auth;
import com.copa.gamestore.security.domain.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import java.math.BigDecimal;

/**
 * @author copa
 * @createDate 2022-09-22 16:39
 * @function
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户服务")
public class UserController {

    @Resource
    private UserApplicationService userApplicationService;

    @ApiOperation("用户注册")
    @PostMapping
    public ResponseVo<User> register(@Validated @RequestBody UserDto userDto) {
        return userApplicationService.register(userDto);
    }

    @RolesAllowed(Role.USER)
    @ApiOperation("用户修改个人信息")
    @PutMapping
    //@Auth
    public ResponseVo<String> put(@Validated @RequestBody UserDto userDto) {
        return userApplicationService.put(userDto);
    }

    @ApiOperation("查询用户余额")
    @GetMapping("account")
    @RolesAllowed(Role.USER)
    public ResponseVo<UserAccount> getAccount() {
        return userApplicationService.account();
    }

    @ApiOperation("充值")
    @PutMapping("recharge")
    @RolesAllowed(Role.ADMIN)
    public ResponseVo<String> recharge(@RequestParam("username") String username,
                                            @RequestParam("money") BigDecimal money) {
        return userApplicationService.recharge(username, money);
    }

    @ApiOperation("重置密码")
    @PutMapping("resetPassword")
    @RolesAllowed(Role.ADMIN)
    public ResponseVo<String> resetPassword(@RequestParam("username") String username) {
        return userApplicationService.resetPassword(username);
    }

    @ApiOperation("个人信息")
    @GetMapping
    @RolesAllowed({Role.ADMIN, Role.USER})
    public ResponseVo<User> get() {
        return userApplicationService.get();
    }

    @ApiOperation("查询用户信息")
    @GetMapping("{id}")
    @RolesAllowed({Role.ADMIN})
    public ResponseVo<User> getUser(@PathVariable("id") Long id) {
        return userApplicationService.getUser(id);
    }
}
